package lanou.a36ke_01.fragmengs.minefragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.media.Image;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import lanou.a36ke_01.R;
import lanou.a36ke_01.activity.LogonActivity;
import lanou.a36ke_01.activity.mineactivity.CollectActivity;
import lanou.a36ke_01.activity.mineactivity.KnowActivity;
import lanou.a36ke_01.activity.mineactivity.SettingsActivity;
import lanou.a36ke_01.activity.mineactivity.UserActivity;
import lanou.a36ke_01.tools.BaseFragment;
import lanou.a36ke_01.tools.MyApp;
import lanou.a36ke_01.tools.MyUser;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private ImageView order;
    private ImageView account;
    private ImageView collect;
    private ImageView certification;
    private ImageView company;
    private ImageView sdim;
    private ImageView know;
    private TextView phone;
    private RelativeLayout title;
    private ImageView settints;
    private MyUser bmobUser;
    private TextView userName;
    private ImageView pic;


    @Override
    protected int setLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        order = bindView(R.id.fragment_mine_image_one);
        account = bindView(R.id.fragment_mine_image_three);
        certification = bindView(R.id.fragment_mine_image_five);
        collect = bindView(R.id.fragment_mine_image_seven);
        company = bindView(R.id.fragment_mine_image_nine);
        sdim = bindView(R.id.fragment_mine_image_ten);
        know = bindView(R.id.fragment_mine_image_tener);
        phone = bindView(R.id.fragment_mine_text_phone);
        title = bindView(R.id.fragment_mine_image_title);
        pic = bindView(R.id.fragment_mine_title_head_pic);
        settints = bindView(R.id.fragment_mine_image_settings);
        userName = bindView(R.id.fragment_mine_title_tv_login);


    }


    @Override
    public void onResume() {
        super.onResume();
        MyUser bmobUser = MyUser.getCurrentUser(MyUser.class);
        if (bmobUser != null) {
            userName.setText(bmobUser.getUsername());
//            Bitmap bitmap = bmobUser.getIcon();
////            Log.d("头像", "myUser.getIcon():" + bmobUser.getIcon());
//            pic.setImageBitmap(bitmap);

        } else {
            //跳转注册
            userName.setText("未登录");
//            pic.setImageBitmap(null);
        }
    }

    @Override
    protected void initData() {

        order.setOnClickListener(this);
        account.setOnClickListener(this);
        certification.setOnClickListener(this);
        collect.setOnClickListener(this);
        company.setOnClickListener(this);
        sdim.setOnClickListener(this);
        know.setOnClickListener(this);
        phone.setOnClickListener(this);
        title.setOnClickListener(this);
        settints.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        /**
         *  BmobUser 是 bmob数据的本地用户缓存
         *  BmobUser.getCurrentUser();方法是获取缓存里的用户信息
         *  假如bmob为空,就说明没登录 反之登录;
         *
         */
        try {
            bmobUser = MyUser.getCurrentUser(MyUser.class);
        } catch (Exception e) {

        }
        if (bmobUser != null) {

            switch (v.getId()) {
                case R.id.fragment_mine_image_tener:
                    Intent know = new Intent(getActivity(), KnowActivity.class);
                    startActivity(know);
                    break;
                case R.id.fragment_mine_text_phone:
                    Intent phone = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + "400-995-3636");
                    phone.setData(data);
                    startActivity(phone);
                    break;
                case R.id.fragment_mine_image_title:
                    Intent title = new Intent(getActivity(), UserActivity.class);
                    startActivity(title);
                    break;
                case R.id.fragment_mine_image_seven:

                    Intent intent = new Intent(getActivity(), CollectActivity.class);
                    startActivity(intent);
                    break;
            }
            // 允许用户使用应用
            Toast.makeText(mContext, "跳进详情页面", Toast.LENGTH_SHORT).show();
        } else {

            // 缓存用户对象为空时， 可打开用户注册界面…
            switch (v.getId()) {

                case R.id.fragment_mine_image_one:
                    Intent order = new Intent(getActivity(), LogonActivity.class);
                    startActivity(order);
                    break;
                case R.id.fragment_mine_image_three:
                    Intent account = new Intent(getActivity(), LogonActivity.class);
                    startActivity(account);
                    break;
                case R.id.fragment_mine_image_five:
                    Intent certification = new Intent(getActivity(), LogonActivity.class);
                    startActivity(certification);
                    break;
                case R.id.fragment_mine_image_seven:
                    Intent collect = new Intent(getActivity(), LogonActivity.class);
                    startActivity(collect);
                    break;
                case R.id.fragment_mine_image_nine:
                    Intent company = new Intent(getActivity(), LogonActivity.class);
                    startActivity(company);
                    break;
                case R.id.fragment_mine_image_ten:
                    Intent sdim = new Intent(getActivity(), LogonActivity.class);
                    startActivity(sdim);
                    break;
                case R.id.fragment_mine_image_tener:
                    Intent know = new Intent(getActivity(), KnowActivity.class);
                    startActivity(know);
                    break;
                case R.id.fragment_mine_text_phone:
                    Intent phone = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + "400-995-3636");
                    phone.setData(data);
                    startActivity(phone);
                    break;
                case R.id.fragment_mine_image_title:
                    Intent title = new Intent(getActivity(), LogonActivity.class);
                    startActivity(title);
                    break;
                case R.id.fragment_mine_image_settings:
                    Intent settings = new Intent(getActivity(), SettingsActivity.class);
                    startActivity(settings);
                    break;
            }
        }
    }

}

